package iscript.uiFramework.util
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.components.window.IWindow;
	import iscript.uiFramework.UIFramework;
	
	/**
	 * ...
	 * @author raymond
	 */
	public class UIComponentUtil
	{
		
		static public function addChild(value:DisplayObject, container:DisplayObjectContainer):void
		{
			if (value is UIComponent)
				UIComponent(value).render(container);			
			else
				container.addChild(value);
		
		}
		
		/**
		 * 移除全部对象,排序UICmp中的 border，background 等物件。
		 * @param	value
		 * @param	container
		 */
		static public function removeAllChild(value:DisplayObject, container:DisplayObjectContainer):void
		{
			for (var i:int=container.numChildren-1; i>=0; i--) 
			{
				//_borderEntity.name = "UIComponent_Border";
				if(container.getChildAt(i).name!="UIComponent_Border"){
					container.removeChildAt(i);	
				}	
			}
		}		
		
		
		/**
		 * 返回全局坐标
		 * @param	target
		 * @return
		 */
		static public function toStagePoint(target:DisplayObject):Point
		{
			var result:Point = new Point();
			while (target != null)
			{
				result.x += target.x;
				result.y += target.y;
				target = target.parent;
			}
			return result;
		}
	
		
		static public function activeDisabled(target:UIComponent):void {
				target.mouseChildren = false;
				target.mouseEnabled = false;
				target.tabChildren = false;
				target.tabEnabled = false;
			}
			
		
		/**
		 * 浮动组件 target:父元素
		 */
		static public function  floatTo(sourceTarget:DisplayObject,cmp:UIComponent):void {		   
			    var pos:Point = UIComponentUtil.toStagePoint(sourceTarget);	
				   //确定X轴MenuPanel的位置在屏幕中
				  cmp.x = pos.x;				  
				   if (  cmp.x < 0) {
					    cmp.x = 0;
					   }
				   if ((cmp.x + cmp.width) > UIFramework.UiStage.stageWidth) {
					     cmp.x = UIFramework.UiStage.stageWidth - cmp.width;
					   }
				  
				  
				  //确定Y轴MenuPanel的位置在屏幕中
				  cmp.y = pos.y + sourceTarget.height - 1;
				  if ((cmp.y + cmp.height) > UIFramework.UiStage.stageHeight) {
					  cmp.y = pos.y - cmp.height;
					  }
			 	UIFramework.UiStage.setChildIndex(cmp, UIFramework.UiStage.numChildren - 1);
			}	
		
	}

}